﻿
var buildingList = {
	printViewSetting : function(){
		var $printView = $("<div/>").attr("id", "dialog-printView").append($("#searchDataList").clone().attr("id", "").find("tbody").empty().end());
		common.dialogPopup($printView, {
	        "닫기": function() {
		          $( this ).dialog( "close" );
		        }
			}, 1000, 700, true);
	},

	printView : function(){
		var datas = $("#searchForm").serialize();
		var $printView = $("#dialog-printView");
		$.post(contextPath+"/allSearch.json", datas , function(data) {

		}, "json").done(function(data) {
			var list = data.list;
			if(list){
				var $tbody =$printView.find("tbody").empty();
				$.each(list, function(i, val){
					var $tr = $("<tr/>")
									.append($("<td/>").text(common.isEmpty(val.building_name)))
									.append($("<td/>").text(common.isEmpty(val.estimat_date)))
									.append($("<td/>").text(common.isEmpty(val.measure_date)))
									.append($("<td/>").text(common.isEmpty(val.measure_target_txt)))
									.append($("<td/>").text(common.isEmpty(val.building_addr)))
									.append($("<td/>").text(common.isEmpty(val.incharge_name)))
									.append($("<td/>").text(common.isEmpty(val.incharge_rank)))
									.append($("<td/>").text(common.isEmpty(val.incharge_tel)))
									.append($("<td/>").text(common.isEmpty(val.incharge_phone)))
									.append($("<td/>").text(common.isEmpty(val.incharge_fax)))
									;
					$tbody.append($tr);
				});
				$printView
					.dialog({ buttons: {
						"출력": function() {
							$("#dialog-printView").printThis();
				        },
				        "닫기": function() {
				        	$( this ).dialog( "close" );
				        }
					} }).dialog( "open" );

			}else{
				$printView.tbDataClearChk();
				common.alert(common.serverError);
			}
		}).fail(function(data) {
			$printView.tbDataClearChk();
			common.alert(common.serverError);
			return false;
		});
	},
	
	dataAllDelete : function(){
		common.confirm("등록된 데이터를 전체 삭제 하시겠습니까?", {
			"전체삭제": function() {
				buildingList.dataDelete();
	        },
	        "취소": function() {
	        	$( this ).dialog( "close" );
	        }
		});
		
		return false;
	},
	
	/**
	 * 데이터 삭제 수행
	 */
	dataDelete : function (){
	
		$.post(contextPath+"/data/delAJax.json", function(data) {
	
		}, "json").done(function(data) {
			if(data.code==0){
				common.alert("삭제되었습니다", function(){
					window.location.reload(true);
				});
			}else if(data.message){
				common.alert(data.message);
			}else{
				common.alert(common.serverError);
			}
		}).fail(function(data) {
			common.alert(common.serverError);
			return false;
		}); 
		return false;
	}

};
$(function(){
	$("#dataAllDelete").click(buildingList.dataAllDelete);
	buildingList.printViewSetting();
	$("#printView").click(buildingList.printView);

	$( "[name=eDateStart]" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 3,
      onClose: function( selectedDate ) {
        $( "[name=eDateEnd]" ).datepicker( "option", "minDate", selectedDate );
      }
    });
    $( "[name=eDateEnd]" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 3,
      onClose: function( selectedDate ) {
        $( "[name=eDateStart]" ).datepicker( "option", "maxDate", selectedDate );
      }
    });
    $( "[name=mDateStart]" ).datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        onClose: function( selectedDate ) {
          $( "[name=mDateEnd]" ).datepicker( "option", "minDate", selectedDate );
        }
      });
      $( "[name=mDateEnd]" ).datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        onClose: function( selectedDate ) {
          $( "[name=mDateStart]" ).datepicker( "option", "maxDate", selectedDate );
        }
      });
});